<?php
    include 'base.php';
    include 'requireLogin.php';
    include 'connectdb.php';        
    require 'Classes/PHPExcel.php';

    $path = $_GET['file'];
    require_once 'Classes/PHPExcel/IOFactory.php';
    $objPHPExcel = PHPExcel_IOFactory::load($path);    
    $success = 0;    
    $fail = 0;
    foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) {
        $worksheetTitle     = $worksheet->getTitle();
        $highestRow         = $worksheet->getHighestRow(); // e.g. 10
        $highestColumn      = $worksheet->getHighestColumn(); // e.g 'F'
        $highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);                        

        echo "<table class=\"table\" style=\"table-layout:fixed; \">";        
        for ($row = 1; $row <= $highestRow;  $row++) {
            $val=array();

            for ($col = 0; $col < $highestColumnIndex; $col++) {                
                $cell = $worksheet->getCellByColumnAndRow($col, $row);                
                if ($col == 6)
                    $val[]= PHPExcel_Style_NumberFormat::toFormattedString($cell->getValue(), "YYYY/M/D");
                else
                    $val[] = $cell->getValue();                            
            }
            if ($row == 1){
                echo "<tr>";
                for ($col = 0; $col < $highestColumnIndex; $col++)
                    echo "<th>".$val[$col]."</th>";
                echo "<th>Tình trạng</th>";
                echo "</tr>";
            }
            else{
                echo "<tr>";
                for ($col = 0; $col < $highestColumnIndex; $col++)
                    echo "<td>".$val[$col]."</td>";                
                $sql = "INSERT INTO product(code, name, infor, import_quantity, import_price, sale_price, date, vendor_code, category) 
                VALUES ('".$val[0]."', '".$val[1]."', '".$val[2]."','".$val[3]."', '".$val[4]."','".$val[5]."', '".$val[6]."', '".$val[7]."','".$val[8]."')";                
                if (mysql_query($sql, $co)){
                        echo "<td>Thành công</td></tr>";
                        $success++;
                    }
                    else {
                        echo "<td>Lỗi</td></tr>";
                        $fail++;
                    }
            }
        }        
        break;
    }
    echo "</table>";
    echo "<ul>";
    echo "<li>Tổng <b>".($success + $fail)." </b></li>";
    echo "<li>Thành công  <b>".$success. " </b></li>";
    echo "<li>Lỗi  <b>".$fail. "</b></li></ul>";
    include 'footer.php';
    ?>    